﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrKHMuaGo : DevComponents.DotNetBar.Office2007Form
    {
        private string MaKH,Day1, Rong1, SLThanh1,Long1;
        
        public FrKHMuaGo()
        {
            InitializeComponent();
        }

        private void FrKHMuaGo_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns["SLThanh"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            //dataGridView1.Columns["ChieuRong"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView2.Columns["SLDu1"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView2.Columns["SLDu2"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            //dataGridView2.Columns["Thanh"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        }
        private void QuyCachThieu()
        {
            try
            {
                Process prs = new Process();
                MaKH = txt_MaKH.Text;
                FormProcess Frp = new FormProcess();
                if (Frp.isfloat(txt_DMGo.Text))
                {
                    float i = float.Parse(txt_DMGo.Text);
                    float DMLong=0;
                    DMLong=float.Parse(txt_DMLong.Text);
                    string select = @"select out.ChieuDaySC,out.ChieuRongSC,out.Long,convert(decimal(10,0),sum(out.MToi)/(1200*" + i;
                    select += @"))  as SLThanh
                                    from (select CTSP.ChieuDaySC,CTSP.ChieuRongSC,CTSP.MaChiTiet,CTSP.TenChiTiet,CTSP.Long,((qry1.SoLuongSP)*(CTSP.SoLuongCT)-isnull(SL.SoLuong,0))*CTSP.ChieuDaiSC as MToi
                                    from (select SanPham.MaSP,SanPham.TenSP,KeHoachSP.SoLuongSP from dbo.KeHoachSP inner join dbo.SanPham on SanPham.MaSP=KeHoachSP.MaSP where MaKeHoach='" + MaKH;
                    select += @"')  as qry1 inner join dbo.ChiTietSP as CTSP
                                    on qry1.MaSP=CTSP.MaSP left join dbo.KH_SLCT as SL on SL.MaChiTiet=CTSP.MaChiTiet
								    where ((qry1.SoLuongSP)*(CTSP.SoLuongCT)-isnull(SL.SoLuong,0))>0) as out
                                    group by out.ChieuDaySC,out.ChieuRongSC,out.Long
                                    having out.ChieuDaySC>0
                                    order by out.ChieuDaySC";
                    DataSet ds = prs.dts(select);
                    float k=0;
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        Long1 = ds.Tables[0].Rows[j]["Long"].ToString();
                        Day1 = ds.Tables[0].Rows[j]["ChieuDaySC"].ToString();
                        Rong1 = ds.Tables[0].Rows[j]["ChieuRongSC"].ToString();
                        SLThanh1 = ds.Tables[0].Rows[j]["SLThanh"].ToString();
                        if (Long1 == "L")
                        {
                            k = float.Parse(ds.Tables[0].Rows[j]["SLThanh"].ToString()) * i / DMLong;
                           
                        }
                        else
                        {
                            k = float.Parse(ds.Tables[0].Rows[j]["SLThanh"].ToString());
                        }
                        int SL = Convert.ToInt32(k);
                        dataGridView1.Rows.Add(Day1, Rong1, SL, Long1);
                    }
                    //dataGridView2.DataSource = ds.Tables[0];
                    ds.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void ChonGo()
        {
            try
            {
                string select = @"select * from dbo.PhachSoChe";
                Process prs = new Process();
                SqlConnection Conn = prs.Connect();
                Conn.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(select, Conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "PhachSoChe");
                int n = dataGridView1.Rows.Count - 2;
                int dslenght = ds.Tables["PhachSoChe"].Rows.Count - 1;
                int i = 0;
                float SLDu1, SLDu2;
                int j = 0;
                string Day_Check = "", Rong_Check = "", CRong1, CRong2, SLMua1, MaPhach1,Long_Check, Day_add;
                float DMLong = float.Parse(txt_DMLong.Text);
                while (i <= n)
                {
                    if (dataGridView1.Rows[i].Cells["Long"].Value.ToString() == "L")
                    {
                        Day_Check = dataGridView1.Rows[i].Cells["ChieuDay"].Value.ToString();
                        Rong_Check = dataGridView1.Rows[i].Cells["ChieuRong"].Value.ToString().Trim();
                        for (j = 0; j <= dslenght; j++)
                        {
                            Long_Check = ds.Tables["PhachSoChe"].Rows[j]["Long"].ToString().Trim();
                            if (Rong_Check == Long_Check)
                            {

                                if (Day_Check == ds.Tables["PhachSoChe"].Rows[j]["ChieuDay"].ToString())
                                {
                                    MaPhach1 = ds.Tables["PhachSoChe"].Rows[j]["MaPhach"].ToString();
                                    CRong1 = ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC1"].ToString();
                                    CRong2 = ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC2"].ToString();
                                    SLMua1 = dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString();
                                    SLDu1 = Convert.ToInt32(float.Parse(SLMua1) * (1 - DMLong));
                                    SLDu2 = SLDu1;
                                    Day_add = dataGridView1.Rows[i].Cells["ChieuDay"].Value.ToString();
                                    dataGridView2.Rows.Add(MaPhach1, SLMua1, Day_Check, CRong1, SLDu1, CRong2, SLDu2);
                                    dataGridView1.Rows.RemoveAt(i);
                                    n--;
                                    break;
                                }
                            }
                        }
                    }
                    i++;
                }
                DuyetSLTon();
                DuyetPt(ds);
                DuyetSLTon();
                DuyetPt(ds);
                DuyetSLTon();
                DuyetPt(ds);
               
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



        }
        private void DuyetSLTon()
        {
            int n = dataGridView1.Rows.Count - 2;
            int i = 0;
            float SLConLai, SLConLaiKH;
            int j = 0;
            int datagrid = dataGridView2.Rows.Count - 2;
            string Day_Check = "", Rong_Check = "", SLGrid1, LongGrid1, CRong1, CRong2, SLMua1, MaPhach1, SLDu1str = "", SLDu2str = "";
            float DMLong = float.Parse(txt_DMLong.Text);
            if (n >= 0)
            {
                i = n;
                while (i >= 0)
                {
                    //MessageBox.Show(dataGridView1.Rows[0].Cells["SLThanh"].Value.ToString());
                    Day_Check = dataGridView1.Rows[i].Cells["ChieuDay"].Value.ToString().Trim();
                    Rong_Check = dataGridView1.Rows[i].Cells["ChieuRong"].Value.ToString().Trim();
                    
                    for (j = datagrid; j>=0; j--)
                    {
                        if (Day_Check == dataGridView2.Rows[j].Cells["CDay"].Value.ToString().Trim())
                        {
                            
                            if (Rong_Check == dataGridView2.Rows[j].Cells["ChieuRongDu1"].Value.ToString().Trim())
                            {
                                
                                SLConLai = float.Parse(dataGridView2.Rows[j].Cells["SLDu1"].Value.ToString()) - float.Parse(dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString());
                                MaPhach1 = dataGridView2.Rows[j].Cells["MaPhach"].Value.ToString();
                                SLMua1 = dataGridView2.Rows[j].Cells["SLMua"].Value.ToString();
                                CRong1 = dataGridView2.Rows[j].Cells["ChieuRongDu1"].Value.ToString();
                                CRong2 = dataGridView2.Rows[j].Cells["ChieuRongDu2"].Value.ToString();
                                SLDu2str = dataGridView2.Rows[j].Cells["SLDu2"].Value.ToString();
                                if (SLConLai >= 0)
                                {
                                    if (dataGridView1.Rows[i].Cells["Long"].Value.ToString().Trim() == "L")
                                    {
                                        SLConLai = float.Parse(dataGridView2.Rows[j].Cells["SLDu1"].Value.ToString()) - float.Parse(dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString()) * (1 - DMLong);
                                    }
                                    
                                    dataGridView1.Rows.RemoveAt(i);
                                    SLDu1str = SLConLai.ToString();
                                    dataGridView2.Rows.Add(MaPhach1, SLMua1, Day_Check, CRong1, SLDu1str, CRong2, SLDu2str);
                                    dataGridView2.Rows.RemoveAt(j);
                                    break;
                                }
                                if (SLConLai < 0)
                                {
                                    SLConLai = -1 * SLConLai;
                                    SLGrid1 = SLConLai.ToString();
                                    if (dataGridView1.Rows[i].Cells["Long"].Value.ToString().Trim() == "L")
                                    {
                                        SLConLaiKH = float.Parse(dataGridView2.Rows[j].Cells["SLDu1"].Value.ToString()) * (1 - DMLong);
                                        SLDu1str = SLConLaiKH.ToString();
                                    }
                                    else
                                        SLDu1str = "0";
                                    dataGridView2.Rows.Add(MaPhach1, SLMua1, Day_Check, CRong1, SLDu1str, CRong2, SLDu2str);
                                    dataGridView2.Rows.RemoveAt(j);
                                    LongGrid1 = dataGridView1.Rows[i].Cells["Long"].Value.ToString();
                                    dataGridView1.Rows.Add(Day_Check, Rong_Check, SLGrid1, LongGrid1);
                                    dataGridView1.Rows.RemoveAt(i);
                                    break;
                                }
                            }
                            if (Rong_Check == dataGridView2.Rows[j].Cells["ChieuRongDu2"].Value.ToString().Trim())
                            {
                                SLConLai = float.Parse(dataGridView2.Rows[j].Cells["SLDu2"].Value.ToString()) - float.Parse(dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString());
                                MaPhach1 = dataGridView2.Rows[j].Cells["MaPhach"].Value.ToString();
                                SLMua1 = dataGridView2.Rows[j].Cells["SLMua"].Value.ToString();
                                CRong1 = dataGridView2.Rows[j].Cells["ChieuRongDu1"].Value.ToString();
                                SLDu1str = dataGridView2.Rows[j].Cells["SLDu1"].Value.ToString();
                                CRong2 = dataGridView2.Rows[j].Cells["ChieuRongDu2"].Value.ToString();
                                

                                if (SLConLai >= 0)
                                {
                                    if (dataGridView1.Rows[i].Cells["Long"].Value.ToString().Trim() == "L")
                                    {
                                        SLConLai = float.Parse(dataGridView2.Rows[j].Cells["SLDu2"].Value.ToString()) - float.Parse(dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString()) * (1 - DMLong);
                                    }
                                    dataGridView1.Rows.RemoveAt(i);
                                    SLDu2str = SLConLai.ToString();
                                    dataGridView2.Rows.Add(MaPhach1, SLMua1, Day_Check, CRong1, SLDu1str, CRong2, SLDu2str);
                                    dataGridView2.Rows.RemoveAt(j);
                                    break;
                                    //MessageBox.Show("test den day" + i.ToString());

                                }
                                if (SLConLai < 0)
                                {
                                    SLConLai = -1 * SLConLai;
                                    SLGrid1 = SLConLai.ToString();
                                    if (dataGridView1.Rows[i].Cells["Long"].Value.ToString().Trim() == "L")
                                    {
                                        SLConLaiKH = float.Parse(dataGridView2.Rows[j].Cells["SLDu2"].Value.ToString()) * (1 - DMLong);
                                        SLDu2str = SLConLaiKH.ToString();
                                    }
                                    else
                                        SLDu2str = "0";
                                    dataGridView2.Rows.Add(MaPhach1, SLMua1, Day_Check, CRong1, SLDu1str, CRong2, SLDu2str);
                                    dataGridView2.Rows.RemoveAt(j);
                                    LongGrid1 = dataGridView1.Rows[i].Cells["Long"].Value.ToString();
                                    dataGridView1.Rows.Add(Day_Check, Rong_Check, SLGrid1, LongGrid1);
                                    dataGridView1.Rows.RemoveAt(i);
                                    break;
                                }
                            }
                        }
                    }
                    //MessageBox.Show("check");
                    i--;
                }
                this.dataGridView1.Sort(this.dataGridView1.Columns["ChieuDay"], ListSortDirection.Ascending);
            }
        }
        private void DuyetPt(DataSet ds)
        {
            int n = dataGridView1.Rows.Count - 2;
            if (n >= 0)
            {
                int dslenght = ds.Tables["PhachSoChe"].Rows.Count - 1;
                string Day_Check = "", Rong_Check = "", CRong1, CRong2, SLMua1, MaPhach1, SLDu1str = "", SLDu2str = "";
                float DMLong = float.Parse(txt_DMLong.Text);
                float SLMuagrid2;
                int count = 0;   
                int i = n,j=0;
                dataGridView1.Rows.Add("0", "0", "0", "0");
                int Batlenh;
                while (i >= 0)
                {

                    Day_Check = dataGridView1.Rows[i].Cells["ChieuDay"].Value.ToString().Trim();
                    Rong_Check = dataGridView1.Rows[i].Cells["ChieuRong"].Value.ToString().Trim();
                    //MessageBox.Show(Rong_Check);
                    SLMuagrid2 = 0;
                    Batlenh = 0;
                    if ((i > 0 && Day_Check == dataGridView1.Rows[i - 1].Cells["ChieuDay"].Value.ToString().Trim() || Day_Check == dataGridView1.Rows[i + 1].Cells["ChieuDay"].Value.ToString()) || (i == 0 && Day_Check == dataGridView1.Rows[i + 1].Cells["ChieuDay"].Value.ToString()))
                    {
                        int check = 0;
                        count = 0;
                        for (j = 0; j <= dslenght; j++)
                        {

                            // MessageBox.Show(ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC1"].ToString() + "" + ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC2"].ToString());
                            if (Day_Check == ds.Tables["PhachSoChe"].Rows[j]["ChieuDay"].ToString().Trim() && (Rong_Check == ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC1"].ToString().Trim() || Rong_Check == ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC2"].ToString().Trim()))
                            {
                                count++;
                                check = j;
                            }
                        }
                        if (count == 1)
                        {
                            MaPhach1 = ds.Tables["PhachSoChe"].Rows[check]["MaPhach"].ToString();
                            CRong1 = ds.Tables["PhachSoChe"].Rows[check]["ChieuRongSC1"].ToString();
                            CRong2 = ds.Tables["PhachSoChe"].Rows[check]["ChieuRongSC2"].ToString();
                            if (ds.Tables["PhachSoChe"].Rows[check]["ChieuRongSC1"].ToString() == ds.Tables["PhachSoChe"].Rows[check]["ChieuRongSC2"].ToString())
                            {
                                SLMua1 = dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString();
                                SLMuagrid2 = float.Parse(SLMua1) / 2;
                                SLDu1str = "0";
                                SLDu2str = "0";
                            }
                            else
                            {
                                if (Rong_Check == CRong1)
                                {
                                    SLMuagrid2 = float.Parse(dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString());
                                    SLDu1str = "0";
                                    if (CRong2 == "0")
                                    {
                                        SLDu2str = "0";
                                    }
                                    else
                                        SLDu2str = SLMuagrid2.ToString();
                                }
                                if (Rong_Check == CRong2)
                                {
                                    SLMuagrid2 = float.Parse(dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString());
                                    SLDu1str = SLMuagrid2.ToString();
                                    SLDu2str = "0";
                                }

                            }
                            dataGridView2.Rows.Add(MaPhach1, SLMuagrid2, Day_Check, CRong1, SLDu1str, CRong2, SLDu2str);
                            dataGridView1.Rows.RemoveAt(i);
                            Batlenh = 1;
                            
                        }
                        if (count == 0)
                        {
                            MaPhach1 = "UnKnown";
                            CRong1 = dataGridView1.Rows[i].Cells["ChieuRong"].Value.ToString();
                            CRong2 = "0";
                            SLMuagrid2 = float.Parse(dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString());
                            SLDu1str = "0";
                            SLDu2str = "0";
                            dataGridView2.Rows.Add(MaPhach1, SLMuagrid2, Day_Check, CRong1, SLDu1str, CRong2, SLDu2str);
                            dataGridView1.Rows.RemoveAt(i);
                            Batlenh = 1;
                        }
                    }
                    if (Batlenh == 0)
                    {
                        if ((i > 0 && Day_Check != dataGridView1.Rows[i - 1].Cells["ChieuDay"].Value.ToString().Trim() && Day_Check != dataGridView1.Rows[i + 1].Cells["ChieuDay"].Value.ToString()) || (i == 0 && Day_Check != dataGridView1.Rows[i + 1].Cells["ChieuDay"].Value.ToString()))
                        {
                            //MessageBox.Show("No khac rui");
                            int check = 0;
                            count = 0;
                            for (j = 0; j <= dslenght; j++)
                            {

                                // MessageBox.Show(ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC1"].ToString() + "" + ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC2"].ToString());
                                if (Day_Check == ds.Tables["PhachSoChe"].Rows[j]["ChieuDay"].ToString().Trim() && (Rong_Check == ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC1"].ToString().Trim() || Rong_Check == ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC2"].ToString().Trim()))
                                {
                                    check = j;
                                    count++;
                                    if (ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC1"].ToString().Trim() == ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC2"].ToString().Trim())
                                    {
                                        count = -1;
                                        break;
                                    }
                                    if (ds.Tables["PhachSoChe"].Rows[j]["ChieuRongSC2"].ToString().Trim() == "0")
                                    {
                                        count = -2;
                                        break;
                                    }
                                }
                            }
                            MaPhach1 = ds.Tables["PhachSoChe"].Rows[check]["MaPhach"].ToString();
                            CRong1 = ds.Tables["PhachSoChe"].Rows[check]["ChieuRongSC1"].ToString();
                            CRong2 = ds.Tables["PhachSoChe"].Rows[check]["ChieuRongSC2"].ToString();
                            if (count == -1)
                            {
                                SLMua1 = dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString();
                                SLMuagrid2 = float.Parse(SLMua1) / 2;
                                SLDu1str = "0";
                                SLDu2str = "0";
                            }
                            if (count == -2)
                            {
                                SLMua1 = dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString();
                                SLMuagrid2 = float.Parse(SLMua1);
                                SLDu1str = "0";
                                SLDu2str = "0";

                            }

                            if (count == 0)
                            {
                                MaPhach1 = "UnKnown";
                                CRong1 = dataGridView1.Rows[i].Cells["ChieuRong"].Value.ToString();
                                CRong2 = "0";
                                SLMuagrid2 = float.Parse(dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString());
                                SLDu1str = "0";
                                SLDu2str = "0";
                            }
                            if (count > 0)
                            {
                                if (Rong_Check == ds.Tables["PhachSoChe"].Rows[check]["ChieuRongSC1"].ToString().Trim())
                                {
                                    CRong1 = ds.Tables["PhachSoChe"].Rows[check]["ChieuRongSC1"].ToString().Trim();
                                    CRong2 = ds.Tables["PhachSoChe"].Rows[check]["ChieuRongSC2"].ToString().Trim();
                                    SLMuagrid2 = float.Parse(dataGridView1.Rows[i].Cells["SLThanh"].Value.ToString());
                                    SLDu1str = "0";
                                    SLDu2str = SLMuagrid2.ToString();
                                }
                            }
                            dataGridView2.Rows.Add(MaPhach1, SLMuagrid2, Day_Check, CRong1, SLDu1str, CRong2, SLDu2str);
                            dataGridView1.Rows.RemoveAt(i);
                        }
                    }
                    i--;
                }

            }
        }
        /*private void DuyetptTrung(DataSet ds)
        {
            int n = dataGridView1.Rows.Count - 2;
            if (n >= 0)
            {
                int dslenght = ds.Tables["PhachSoChe"].Rows.Count - 1;
                string Day_Check = "", Rong_Check = "", CRong1, CRong2, SLMua1, MaPhach1, SLDu1str = "", SLDu2str = "";
                float DMLong = float.Parse(txt_DMLong.Text);
                int count = 0;
                int i = n, j = 0;
                dataGridView1.Rows.Add("0", "0", "0", "0");
                float ptmin = 0;
                while (i >= 0)
                {
                    Day_Check = dataGridView1.Rows[i].Cells["ChieuDay"].Value.ToString().Trim();
                    Rong_Check = dataGridView1.Rows[i].Cells["ChieuRong"].Value.ToString().Trim();
                    
                }

            }

        }*///Hiện tại trường hợp này chưa xảy ra trong CSDL nên tạm thời chưa cần xét đến
        private void bt_Search_Click(object sender, EventArgs e)
        {
            try
            {
                float i = float.Parse(txt_DMGo.Text);
                float j = float.Parse(txt_DMLong.Text);

                if (i > 0 && i < 1&&j>0&&j<1)
                {
                    QuyCachThieu();
                    ChonGo();
                }
                else
                    MessageBox.Show("Giá trị nhập 0<x<1");
            }
            catch (Exception ex)
            {
                string s = "Input string";
                if (ex.Message.ToString().Contains(s))
                {
                    MessageBox.Show("Giá trị nhập phải là số");
                    txt_DMGo.Text = "";
                    txt_DMLong.Text = "";
                }
                else
                    MessageBox.Show(ex.Message);
            }
        }
        
        
    }
}
